<?php
/**
 * @author Eugenio Fage
 * http://eugeniofage.wordpress.com/
 * http://code.google.com/p/monky/
 * 
 */
class CommentsController extends AppController {

	var $name = 'Comments';
	var $uses =array('Comment','Log');
	var $components=array('RequestHandler');
	

	var $paginate=array('Comment'=>array(
			'fields'=>array('id','created','votes','karma','content'),				
			'contain'=>array(
				'User'=>array('username','karma')				
			),
			'order'=>array('Comment.id'=>'ASC'),
			'limit' => 20
		));
	
	function addComment($link_id=null){
		$this->disableCache();
		$this->helpers[]='Ajax';
		$this->autoRender = false;
		
		if(!$link_id || !$this->Comment->Link->hasAny(array('id'=>$link_id))){
			exit;
		}
   		
		$user_id=$this->Auth->user('id');
		try{
			$this->Comment->User->checkCanPostComment($user_id,$link_id);	
			if (!empty($this->data)) {
   				$content=$this->data['Comment']['content'];
   				$ip=Visitor::ip_int();
   				
  				$this->Comment->create(compact('content','link_id','user_id','ip'));

   				if ($this->Comment->save(null,true,array('content','link_id','user_id','ip'))) {
   					$this->Log->save(array('Log'=>array('type'=>'comment_new','ref_id'=>$link_id,'user_id'=>$user_id,'extradata'=>$this->Comment->id)));
   					
   					$this->render('commentsave');
   					return;
   				}
  			}
  			$this->set(compact('link_id'));
		}catch(CommentException $e){
  			$this->set('error',$e->getMessage());
		}
  		$this->render('/elements/comments/commentform');
		
	}
	
	function viewComments($link_id=null){
		$this->disableCache();
		$this->helpers[]='Ajax';
		$this->autoRender = false;
		
		if(!$link_id || !$this->Comment->Link->hasAny(array('id'=>$link_id))){
			exit;
		}elseif(!$this->RequestHandler->isAjax()){
			$this->redirect(array('controller'=>'links','action'=>'view','id'=>$link_id));
		}
   		$link=$this->Comment->Link->find('first',array(
   			'fields'=>array('Link.id','Link.comment_count'),
   			'conditions'=>array('Link.id'=>$link_id),
   			'recursive'=>-1
   		));
		$comments=$this->paginate('Comment');
   		$comments=$this->Comment->setCanVoteComments($comments,$this->Auth->user('id'));

   		
		try{
			$this->Comment->User->checkCanPostComment($this->Auth->user('id'),$link_id);
		}catch (CommentException $e){
			$this->set('error',$e->getMessage());
		}
		
  		$this->set(compact('link','comments'));
  		$this->render('/elements/comments/comments');
	}
	
	function redirectToLinkComment($id=null){
		$id=max(0,$id);
		$comment=$this->Comment->find('first',array(
			'fields'=>array('Comment.id'),
			'contain'=>array('Link'=>array('id','title')),
			'conditions'=>array('Comment.id'=>$id)
		));
		
		if(empty($comment)){
			$this->redirect(array('controller'=>'links','action'=>'index'));
		}
				
		extract($comment);
						
		$totalCommentsBeforeThisComment=$this->Comment->find('count',array(
			'conditions'=>array(
				'Comment.id<'.$Comment['id'],
				'Comment.link_id'=>$Link['id']
			),'recursive'=>-1
		));
				
		$page=intval(ceil($totalCommentsBeforeThisComment) / $this->paginate['Comment']['limit'])+1;
				
		$this->redirect(array(
			'controller'=>'links',
			'action'=>'view',
			'slug'=>Inflector::slug($Link['title']),
			'id'=>$Link['id'],
			'page'=>$page,
			'#'=>('comment-'.$Comment['id'])
		));		
	}	
	
	function doVote(){
		if(empty($this->params['form'])|| !isset($this->params['form']['id'])) exit;
					
		$vote=array(
			'User'=>array(
				'id'=>(int)$this->Auth->user('id'),
				'vote'=>$this->params['form']['value']
			),'Comment'=>array('id'=>$this->params['form']['id'])
		);
						
		try{
			$this->set('vote_result',$this->Comment->vote($vote));
		}catch(CommentVoteException $exception){
			$this->set('vote_result',array('error'=>$exception->getMessage()));
		}
		
		$this->layout = 'json';
		$this->render('json/vote');
	}
		
}